package com.ruoyi.receipt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.receipt.domain.TbSubjectCluste;
import com.ruoyi.receipt.utils.StatisAnalysisUtils;
import com.ruoyi.receipt.utils.UserInitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.receipt.mapper.TbExampleSubjectManagerMapper;
import com.ruoyi.receipt.domain.TbExampleSubjectManager;
import com.ruoyi.receipt.service.ITbExampleSubjectManagerService;

/**
 * 例题题目管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-29
 */
@Service
public class TbExampleSubjectManagerServiceImpl implements ITbExampleSubjectManagerService 
{
    @Autowired
    private TbExampleSubjectManagerMapper tbExampleSubjectManagerMapper;

    @Autowired
    private StatisAnalysisUtils statisAnalysisUtils;

    /**
     * 查询例题题目管理
     * 
     * @param id 例题题目管理ID
     * @return 例题题目管理
     */
    @Override
    public TbExampleSubjectManager selectTbExampleSubjectManagerById(Long id)
    {
        return tbExampleSubjectManagerMapper.selectTbExampleSubjectManagerById(id);
    }

    /**
     * 查询例题题目管理列表
     * 
     * @param tbExampleSubjectManager 例题题目管理
     * @return 例题题目管理
     */
    @Override
    @DataScope(deptAlias = "da",userAlias = "createId")
    public List<TbExampleSubjectManager> selectTbExampleSubjectManagerList(TbExampleSubjectManager tbExampleSubjectManager)
    {
        return tbExampleSubjectManagerMapper.selectTbExampleSubjectManagerList(tbExampleSubjectManager);
    }

    /**
     * 新增例题题目管理
     * 
     * @param tbExampleSubjectManager 例题题目管理
     * @return 结果
     */
    @Override
    public int insertTbExampleSubjectManager(TbExampleSubjectManager tbExampleSubjectManager)
    {
        tbExampleSubjectManager.setCreateTime(DateUtils.getNowDate());
        UserInitUtils.initUserInfo(tbExampleSubjectManager);
        return tbExampleSubjectManagerMapper.insertTbExampleSubjectManager(tbExampleSubjectManager);
    }

    /**
     * 修改例题题目管理
     * 
     * @param tbExampleSubjectManager 例题题目管理
     * @return 结果
     */
    @Override
    public int updateTbExampleSubjectManager(TbExampleSubjectManager tbExampleSubjectManager)
    {
        tbExampleSubjectManager.setUpdateTime(DateUtils.getNowDate());
        UserInitUtils.initUserInfoForUpdate(tbExampleSubjectManager);
        return tbExampleSubjectManagerMapper.updateTbExampleSubjectManager(tbExampleSubjectManager);
    }

    /**
     * 批量删除例题题目管理
     * 
     * @param ids 需要删除的例题题目管理ID
     * @return 结果
     */
    @Override
    public int deleteTbExampleSubjectManagerByIds(Long[] ids)
    {
        return tbExampleSubjectManagerMapper.deleteTbExampleSubjectManagerByIds(ids);
    }

    /**
     * 删除例题题目管理信息
     * 
     * @param id 例题题目管理ID
     * @return 结果
     */
    @Override
    public int deleteTbExampleSubjectManagerById(Long id)
    {
        return tbExampleSubjectManagerMapper.deleteTbExampleSubjectManagerById(id);
    }
    /**
     * 统计所有未删除的例题个数
     *
     * @return 结果
     */
    @Override
    public int selectCountAllSubjecttManager(){
        return tbExampleSubjectManagerMapper.selectCountAllSubjecttManager();
    }
    @Override
    public List<TbSubjectCluste> selectClName(String title) {
        return tbExampleSubjectManagerMapper.selectClName(title);
    }

    /**
     * 查询例题难度
     * @param id
     * @return
     */
    @Override
    public Long selectdiffctRatio(Long id) {
        return tbExampleSubjectManagerMapper.selectdiffctRatio(id);
    }
    /**
     * 审核通过
     */
    @Override
    public int updateTbExampleSubjectManagerStatus(TbExampleSubjectManager tbExampleSubjectManager)
    {

        //获取现在的id
        Long id = tbExampleSubjectManager.getId();
        //获取现在的难度
        //根据id获取
        TbExampleSubjectManager tbExampleSubjectManager1 = tbExampleSubjectManagerMapper.selectTbExampleSubjectManagerById(id);
        Long diffictRatio = tbExampleSubjectManager1.getDiffictRatio();
        if (diffictRatio >=35){
            statisAnalysisUtils.putDiffictRatioGreater35(id);
        }
        if (diffictRatio <35){
            Boolean isExitDiffictRatioGreater35 = statisAnalysisUtils.getIsExitDiffictRatioGreater35(id);
            if (isExitDiffictRatioGreater35 ){
                statisAnalysisUtils.deleteDiffictRatioGreater35(id);
            }
        }
        //推入题簇和例题关系
        List<Long> longs=new ArrayList<>();
        longs.add(tbExampleSubjectManager1.getId());
        Long subjectClusteId = tbExampleSubjectManager1.getSubjectClusteId();
        statisAnalysisUtils.putClusteSubjecRelation(subjectClusteId,longs);
        tbExampleSubjectManager.setUpdateTime(DateUtils.getNowDate());
        return tbExampleSubjectManagerMapper.updateTbExampleSubjectManager(tbExampleSubjectManager);
    }

    @Override
    public List<Long> selectClusteId(Long knowledgeManagerId) {
        return tbExampleSubjectManagerMapper.selectClusteId(knowledgeManagerId);
    }

    @Override
    public List<TbExampleSubjectManager> selectTbExampleSubjectManagerstatus(TbExampleSubjectManager tbExampleSubjectManager) {
        return tbExampleSubjectManagerMapper.selectTbExampleSubjectManagerstatus(tbExampleSubjectManager);
    }
}
